package com.example.administrator.test;

import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;


import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private MyApplication app1;
    private MyApplication app2;

    private long firstTime = 0;
    private ImageView title_image;
    private TextView title_text;

    ImageView tab1_image;
    ImageView tab2_image;
    ImageView tab3_image;
    ImageView tab4_image;

    TextView tab1_text;
    TextView tab2_text;
    TextView tab3_text;
    TextView tab4_text;

    RelativeLayout tab1;
    RelativeLayout tab2;
    RelativeLayout tab3;
    RelativeLayout tab4;

    FrameLayout frameLayout;
    FragmentManager fragmentManager;
    FragmentTransaction transaction;
    Fragment oneFragment;
    Fragment twoFragment;
    Fragment threeFragment;
    Fragment fourFragment;

    View view;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ActionBar bar = getSupportActionBar();
        if(bar!=null){
            bar.hide();
        }
        initview();
        fragmentManager = getSupportFragmentManager();
        setItem(0);
        initData();//初始化数据
    }

    private void initData() {
        app1 = new MyApplication();
        DbManager db = x.getDb(app1.getDaoConfig());
        try {
            Book book = db.selector(Book.class).findFirst();
            if(book == null){
                for (int i=1;i<=30;i++){
                    Book temp = new Book();
                    temp.setImg("book_"+i);
                    temp.setTitle("book_"+i);
                    db.save(temp);
                }
            }
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    private void initview() {

        view = (View)findViewById(R.id.activity_title);
        title_image = (ImageView)view.findViewById(R.id.index_title_image);
        title_text = (TextView)findViewById(R.id.index_title_text);
        title_image.setImageResource(R.mipmap.ic_launcher);
        title_text.setText(this.getResources().getString(R.string.main_title));

        title_image.setOnClickListener(this);

        tab1_text = (TextView)findViewById(R.id.first_text);
        tab2_text = (TextView)findViewById(R.id.second_text);
        tab3_text = (TextView)findViewById(R.id.third_text);
        tab4_text = (TextView)findViewById(R.id.fourth_text);

        tab1_image = (ImageView)findViewById(R.id.first_image);
        tab2_image = (ImageView)findViewById(R.id.second_image);
        tab3_image = (ImageView)findViewById(R.id.third_image);
        tab4_image = (ImageView)findViewById(R.id.fourth_image);


        tab1 = (RelativeLayout)findViewById(R.id.first_layout);
        tab2 = (RelativeLayout)findViewById(R.id.second_layout);
        tab3 = (RelativeLayout)findViewById(R.id.third_layout);
        tab4 = (RelativeLayout)findViewById(R.id.fourth_layout);
        tab1.setOnClickListener(this);
        tab2.setOnClickListener(this);
        tab3.setOnClickListener(this);
        tab4.setOnClickListener(this);


    }

    public boolean onKeyDown(int keyCode, KeyEvent event){
        if(keyCode==KeyEvent.KEYCODE_BACK&&event.getRepeatCount()==0){
            if(System.currentTimeMillis()-firstTime>2000){
                Toast.makeText(MainActivity.this,"双击退出",Toast.LENGTH_SHORT).show();
                firstTime = System.currentTimeMillis();
            }else{
                finish();
                System.exit(0);
            }
        }
        return false;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId())
        {
            case R.id.first_layout:
                setItem(0);
                break;
            case R.id.second_layout:
                setItem(1);
                break;
            case R.id.third_layout:
                setItem(2);
                break;
            case R.id.fourth_layout:
                setItem(3);
                break;
            case R.id.index_title_image:
                Intent phone = new Intent();
                phone.setAction(Intent.ACTION_CALL);
                phone.setData(Uri.parse("tel:1380013800"));
                startActivity(phone);
//                app2 = (MyApplication)getApplication();
//                Toast.makeText(MainActivity.this, app2.getAccount(), Toast.LENGTH_SHORT).show();
//                Intent web = new Intent();
//                web.setAction(Intent.ACTION_VIEW);
//                web.setData(Uri.parse("http://www.imooc.com/"));
//                startActivity(web);
                break;
        }
    }

    public void setItem(int index)
    {

        transaction = fragmentManager.beginTransaction();
        //重置选项
        clearItem();
        //隐藏Fragment
        hideFragment(transaction);
        switch (index)
        {
            case 0:
                title_text.setText("主页");
                tab1_image.setImageResource(R.mipmap.tab11);
                tab1_text.setTextColor(android.graphics.Color.rgb(63,81,181));
                if(oneFragment==null){
                    oneFragment = new OneFragment();
                    transaction.add(R.id.fragmLayout,oneFragment);
                }else{
                    transaction.show(oneFragment);
                }
                break;

            case 1:
                title_text.setText("推荐");
                tab2_image.setImageResource(R.mipmap.tab21);
                tab2_text.setTextColor(android.graphics.Color.rgb(63,81,181));
                if(twoFragment==null){
                    twoFragment = new TwoFragment();
                    transaction.add(R.id.fragmLayout,twoFragment);
                }else{
                    transaction.show(twoFragment);
                }
                break;

            case 2:
                title_text.setText("收藏");
                tab3_image.setImageResource(R.mipmap.tab31);
                tab3_text.setTextColor(android.graphics.Color.rgb(63,81,181));
//                if(threeFragment==null){
//                    threeFragment = new ThreeFragment();
//                    transaction.add(R.id.fragmLayout,threeFragment);
//                }else{
//                    transaction.show(threeFragment);
//                }
                threeFragment = new ThreeFragment();
                transaction.add(R.id.fragmLayout,threeFragment);
                break;

            case 3:
                title_text.setText("我的");
                tab4_image.setImageResource(R.mipmap.tab41);
                tab4_text.setTextColor(android.graphics.Color.rgb(63,81,181));
                if(fourFragment==null){
                    fourFragment = new FourFragment();
                    transaction.add(R.id.fragmLayout,fourFragment);
                }else{
                    transaction.show(fourFragment);
                }
                break;
        }
        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction) {
        if (oneFragment!=null){
            transaction.hide(oneFragment);
        }
        if(twoFragment!=null){
            transaction.hide(twoFragment);
        }
        if(threeFragment!=null){
            transaction.hide(threeFragment);
        }
        if(fourFragment!=null){
            transaction.hide(fourFragment);
        }
    }

    //清空充值选项
    private void clearItem() {
//        tab1.setBackgroundColor(Color.WHITE);
//        tab2.setBackgroundColor(Color.WHITE);
//        tab3.setBackgroundColor(Color.WHITE);
//        tab4.setBackgroundColor(Color.WHITE);
        tab1_image.setImageResource(R.mipmap.tab12);
        tab2_image.setImageResource(R.mipmap.tab22);
        tab3_image.setImageResource(R.mipmap.tab32);
        tab4_image.setImageResource(R.mipmap.tab42);

        tab1_text.setTextColor(android.graphics.Color.rgb(117,151,179));
        tab2_text.setTextColor(android.graphics.Color.rgb(117,151,179));
        tab3_text.setTextColor(android.graphics.Color.rgb(117,151,179));
        tab4_text.setTextColor(android.graphics.Color.rgb(117,151,179));

    }


}
